package com.yibo.user.controller;

import org.apache.ibatis.io.Resources;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

import static com.yibo.support.constant.ApiUrlConstant.PUBLIC_URL;

@Controller
@RequestMapping(PUBLIC_URL+"/test")
public class TestController {


    @GetMapping("/2123")
    public String loadHtml(HttpServletResponse response){
        try{
            File file = Resources.getResourceAsFile("model/2123.html");
            InputStream fis = new BufferedInputStream(new FileInputStream(file));
            response.reset();
            response.setContentType("application/x-download");
            response.addHeader("Content-Disposition","attachment;filename="+ new String("2123.html"));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream out = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            byte[] buffer = new byte[1024 * 1024 * 10];
            int i = -1;
            while ((i = fis.read(buffer)) != -1) {
                out .write(buffer, 0, i);
            }
            fis.close();
            out .flush();
            out .close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
